//
//  UIView+WDExtention.m
//  MyDemos
//
//  Created by iMac on 2018/3/14.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "UIView+WDExtention.h"
#import <objc/runtime.h>

const char *clickViewKey = "clickViewKey";
@implementation UIView (WDExtention)

/// set frame
- (UIView *(^)(CGRect))sFrame {
    __weak typeof(self)weekSelf = self;
    return ^UIView *(CGRect frame) {
        [weekSelf setFrame:frame];
        return self;
    };
}

/// set background color
- (UIView *(^)(UIColor *))sBGColor {
    __weak typeof(self)weekSelf = self;
    return ^UIView *(UIColor *color) {
        [weekSelf setBackgroundColor:color];
        return self;
    };
}

/// add subview
- (UIView *(^)(UIView *))aSubView {
    __weak typeof(self)weekSelf = self;
    return ^UIView *(UIView *view) {
        [weekSelf addSubview:view];
        return self;
    };
}

/// tap gesture
- (void)tap:(void(^)(UIView *view))callback {
    self.tapCallback = callback;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
    [gesture addTarget:self action:@selector(viewClick:)];
    [self addGestureRecognizer:gesture];
}
- (void)viewClick:(UIView *)sender {
    if (self.tapCallback) {
        self.tapCallback(sender);
    }
}
- (void (^)(UIView *))tapCallback {
    return objc_getAssociatedObject(self, clickViewKey);
}
- (void)setTapCallback:(void (^)(UIView *))tapCallback {
    objc_setAssociatedObject(self, clickViewKey, tapCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
